package com.vito.dcs.statistics.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.BadSqlGrammarException;

import com.alibaba.dubbo.config.annotation.Service;
import com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException;
import com.vito.dcs.common.util.SplitTableUtil;
import com.vito.dcs.statistics.dao.SectionStatisticsMapper;
import com.vito.dcs.statistics.service.SectionStatisticsService;

@Service
public class SectionStatisticsServiceImpl implements SectionStatisticsService{

	@Autowired
	private SectionStatisticsMapper sectionStatisticsMapper;

	@Override
	public Map<String, Object> querySectionStatistics(
			Map<String, Object> map) {

		if("1".equals(map.get("paramKey"))){
			Calendar c = Calendar.getInstance();
			String endTime = ""+ c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH)+1 < 10 ? "0"+(c.get(Calendar.MONTH)+1) : c.get(Calendar.MONTH)+1) + "-" + c.get(Calendar.DATE);
			map.put("endTime", endTime);
			c.add(Calendar.DATE,-2);
			String startTime = ""+ c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH)+1 < 10 ? "0"+(c.get(Calendar.MONTH)+1) : c.get(Calendar.MONTH)+1) + "-" + c.get(Calendar.DATE);
			map.put("startTime", startTime);
		} else if("2".equals(map.get("paramKey"))){
			Calendar c = Calendar.getInstance();
			String endTime = ""+ c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH)+1 < 10 ? "0"+(c.get(Calendar.MONTH)+1) : c.get(Calendar.MONTH)+1) + "-" + c.get(Calendar.DATE);
			map.put("endTime", endTime);
			c.add(Calendar.MONTH,-1);
			String startTime = ""+ c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH)+1 < 10 ? "0"+(c.get(Calendar.MONTH)+1) : c.get(Calendar.MONTH)+1) + "-" + c.get(Calendar.DATE);
			map.put("startTime", startTime);
		}
		//List<Map<String, Object>> querySectionStatistics = sectionStatisticsMapper.querySectionStatistics(map);
		//Map<String, Object> querySectionStatistics_interval = sectionStatisticsMapper.querySectionStatistics_interval(map);
		//querySectionStatistics.add(querySectionStatistics_interval);
		return null;
	}
	/**
	 * 当前采用方法
	 */
	@Override
	public Map<String, Object> querySectionStatistics_new(
			Map<String, Object> map) {
		
		String tableName = SplitTableUtil.getTableName(new Date());
		map.put("tableName", tableName);
		
		Map<String, Object> resMap = new HashMap<String, Object>();
		List<Map<String, Object>> queryLayer = sectionStatisticsMapper.queryLayer(map);
		if("1".equals(map.get("paramKey"))){

			Calendar c = Calendar.getInstance();
			String endTime = ""+ c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH)+1 < 10 ? "0"+(c.get(Calendar.MONTH)+1) : c.get(Calendar.MONTH)+1) + "-" + c.get(Calendar.DATE);
			map.put("endTime", endTime);
			c.add(Calendar.DATE,-2);
			String startTime = ""+ c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH)+1 < 10 ? "0"+(c.get(Calendar.MONTH)+1) : c.get(Calendar.MONTH)+1) + "-" + c.get(Calendar.DATE);
			map.put("startTime", startTime);
			if(queryLayer != null && queryLayer.size() > 0){
				processData(map, resMap, queryLayer);
			}


		} else if("2".equals(map.get("paramKey"))){
			Calendar c2 = Calendar.getInstance();
			String endTime2 = ""+ c2.get(Calendar.YEAR) + "-" + (c2.get(Calendar.MONTH)+1 < 10 ? "0"+(c2.get(Calendar.MONTH)+1) : c2.get(Calendar.MONTH)+1) + "-" + c2.get(Calendar.DATE);
			map.put("endTime", endTime2);
			c2.add(Calendar.MONTH,-1);
			String startTime2 = ""+ c2.get(Calendar.YEAR) + "-" + (c2.get(Calendar.MONTH)+1 < 10 ? "0"+(c2.get(Calendar.MONTH)+1) : c2.get(Calendar.MONTH)+1) + "-" + c2.get(Calendar.DATE);
			map.put("startTime", startTime2);
			
			if(queryLayer != null && queryLayer.size() > 0){
				processData(map, resMap, queryLayer);
			}
		}

		return resMap;
	}
	private void processData(Map<String, Object> map,
			Map<String, Object> resMap, List<Map<String, Object>> queryLayer) {
		for (Map<String, Object> map2 : queryLayer) {
			map.put("layerId", map2.get("layer_id"));
			map.put("p_start", map2.get("start_time"));
			map.put("p_end", map2.get("end_time"));
			try {
				Map<String, Object> statistics_day = sectionStatisticsMapper.querySectionStatistics(map);
				if(statistics_day != null){
					statistics_day.put("tableName", map.get("tableName"));
					statistics_day.put("id", statistics_day.get("startId"));
					Map<String, Object> start = sectionStatisticsMapper.querySectionStatisticsById(statistics_day);
					statistics_day.put("id", statistics_day.get("endId"));
					Map<String, Object> end = sectionStatisticsMapper.querySectionStatisticsById(statistics_day);
					
					Map<String,Object> mm = new HashMap<String, Object>();
					mm.putAll(map2);
					mm.put("lngStart", start.get("lng"));
					mm.put("lngEnd", end.get("lng"));
					mm.put("latStart", start.get("lat"));
					mm.put("latEnd", end.get("lat"));
					resMap.put((String) map2.get("layer_name"), mm);
				}
			} catch (Exception e) {
				BadSqlGrammarException  b = (BadSqlGrammarException) e;
				Throwable cause = b.getCause();
				MySQLSyntaxErrorException m = null;
				try {
					 m = (MySQLSyntaxErrorException) cause;
				} catch (Exception e2) {
					throw e;
				}
				if(m != null && m.getErrorCode() == 1146){
					try {
						
						Map<String,Object> mm = new HashMap<String, Object>();
						mm.putAll(map2);
						resMap.put((String) map2.get("layer_name"), mm);
						
					} catch (Exception e2) {
						throw e2;
					}
					
				} else {
					throw e;
				}
			}
			
		}
	}

	@Override
	public List<Map<String, Object>> querySectionStatistics2(Map<String, Object> map) {

		List<Map<String, Object>> list = sectionStatisticsMapper.querySectionStatistics2(map);
		if(list.size() > 0){
			for(int i = 0; i < list.size(); i ++){
				if(list.get(i).get("yId") != null){
					map.put("layerId", list.get(i).get("yId"));
					int queryNumOfCon = sectionStatisticsMapper.queryNumOfCon(map);
					list.get(i).put("distance", queryNumOfCon);
				}
			}
		}

		return list;
	}
	@Override
	public List<Map<String, Object>> queryLayer(Map<String, Object> map) {
		return sectionStatisticsMapper.queryLayer(map);
	}

}
